package com.bjxst.web.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/*退出登录
当HttpSession销毁之前会启动Listener监听
* 在listener 中的HttpSessionLifecycleListener.java（解决HttpSession超时，被反复销毁时的异常问题）在销毁session之前已经删除了session*/
@WebServlet("/logout.do")
public class logoutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //取到session对象  销毁它  在Listener中的的HttpSessionLifecycleListener.java就会删除session
        HttpSession session = req.getSession();
        session.invalidate();//销毁
        //重定向跳转到登录页面
        resp.sendRedirect("login.jsp");
    }
}
